package xsdcl;

import java.io.BufferedReader;
//import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.Enumeration;
//import java.util.List;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

//import org.apache.xerces.xni.XNIException;
//import org.apache.xerces.xni.parser.XMLInputSource;
import org.wiztools.xsdgen.ParseException;
import org.wiztools.xsdgen.XsdGen;

import com.conradroche.matra.Matra;
import com.conradroche.matra.decl.ElementType;
import com.conradroche.matra.dtdparser.DTDParser;
import com.conradroche.matra.exception.DTDException;
import com.conradroche.matra.io.DTDFile;
import com.conradroche.matra.io.DTDSource;
import com.conradroche.matra.tree.DTDTree;
//import com.skywing.dtd2xsd.DTD2XSDWriter;
//import com.skywing.dtd2xsd.XsdTypePattern;

public class SchemaParser
{
	public Document FromXML(String xmlFile) throws IOException, ParseException
	{
		final XsdGen xsdGen = new XsdGen();
		xsdGen.parse(new File(xmlFile));
		
		return xsdGen.GetDocument();
	}
	
//	public Document FromDTD(String dtdFile, String targetNamespace, List<XsdTypePattern> listXsdTypePattern) 
//							throws ValidityException, ParsingException, IOException
//	{
//		DTD2XSDWriter writer = new DTD2XSDWriter();
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
//		writer.setTargetNamespace(targetNamespace);
//		writer.addXsdTypePattern(listXsdTypePattern);
//		writer.setOutStream(baos);
//		
//		try {
//			writer.parse(new XMLInputSource(null, dtdFile, null));
//		} catch (XNIException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//		String xsdStr = baos.toString();
//		System.out.println(xsdStr);
//		return Str2Doc(xsdStr);		
//	}
	
	public ElementType FromDTDFile(String dtdFile) throws DTDException
	{
		Matra matra = new Matra();
		DTDParser parser = new DTDParser();
		DTDSource dtdSrc = new DTDFile(dtdFile);
		parser.parse(dtdSrc);
		
		DTDTree tree = new DTDTree(parser);
		
		Enumeration roots = parser.getDocType().getRootElements();
		ElementType dtdRoot = null;
		if (roots != null && roots.hasMoreElements())
		{
			String rootElementName = (String) roots.nextElement();
			dtdRoot = (ElementType) tree.ElementList
					.get(rootElementName);
		}
		return dtdRoot;
	}
	
	public ElementType FromDTDString(String dtdString) throws DTDException
	{
		Matra matra = new Matra();
		DTDParser parser = new DTDParser();
		parser.parse(dtdString);
		
		DTDTree tree = new DTDTree(parser);
		
		Enumeration roots = parser.getDocType().getRootElements();
		ElementType dtdRoot = null;
		if (roots != null && roots.hasMoreElements())
		{
			String rootElementName = (String) roots.nextElement();
			dtdRoot = (ElementType) tree.ElementList
					.get(rootElementName);
		}
		return dtdRoot;
	}
	
	public Document FromXSD(String xsdFile) throws ValidityException, ParsingException, IOException
	{
		File file = new File(xsdFile);
        BufferedReader reader = null;
        String xsdStr = "";
        try {
            //reader = new BufferedReader(new FileReader(file));
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(xsdFile),"utf-8"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
            	xsdStr = xsdStr + tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
		return Str2Doc(xsdStr);
	}
	
	private Document Str2Doc(String xsdStr) throws ValidityException, ParsingException, IOException
	{
		System.out.println(xsdStr);
		Document doc ;
		Builder builder =  new Builder();
		doc = builder.build(xsdStr, null);
		return doc;
	}
	
}
